﻿using UnityEngine;
using System.Collections;
using Dict.Config;
using Dict.Blo;
using Dict.DataModel;

namespace UI
{
    public class HeroIcon : MonoBehaviour
    {
        public UISprite iconBg;
        public UISprite iconFrame;
        public TextureKit heroIcon;
        public UILabel heroLevel;
        public UILabel labNum;
        public GameObject levelBg;
        public GameObject chosedBg;
        public GameObject selectMark;
        public GameObject fragFrame;
        public GameObject redPoint;
        public UILabel fragNum;
        public GameObject onArrayMark;
        public UILabel onArrayLabel;
        public GameObject goodAt;

        public delegate void OnIconClick(string guid);
        public OnIconClick m_callback;
        
        string heroGuid;
        public string HeroGuid
        {
            get
            {
                return heroGuid;
            }
        }

        public void SetHero(string guid)
        {
            heroGuid = guid;
            UIHero hero = StaticData.playerData.GetUIHero(guid);
            if(hero == null)
            {
                return;
            }
            
            QualityValue quality = QualityValue.WHITE;
            
            quality = (QualityValue)hero.heroData.quality;
            heroIcon.iconType = TextureIcon.HERO;
            heroIcon.textureName = hero.heroData.iconName;

            //隐藏等级、碎片、已上阵等
            levelBg.SetActive(false);
            fragFrame.SetActive(false);
            onArrayMark.SetActive(false);
            goodAt.SetActive(false);
            SetQualityColor(quality);
        }

        public void SetHeroWithId(string id)
        {
            heroGuid = id;
            TableHero heroData = DictHeroBlo.GetTableHero(id);

            if (heroData == null)
                return;

            QualityValue quality = QualityValue.WHITE;

            quality = (QualityValue)heroData.quality;
            heroIcon.iconType = TextureIcon.HERO;
            heroIcon.textureName = heroData.iconName;

            //隐藏等级、碎片
            levelBg.SetActive(false);
            fragFrame.SetActive(false);
            onArrayMark.SetActive(false);
            goodAt.SetActive(false);
            SetQualityColor(quality);
        }

        public void SetFrag(UIFragItem frag)
        {
            heroGuid = frag.fragData.id;
            TableHero heroData = DictHeroBlo.GetTableHero(frag.fragData.target);

            if (heroData == null)
                return;

            QualityValue quality = QualityValue.WHITE;

            quality = (QualityValue)heroData.quality;
            heroIcon.iconType = TextureIcon.HERO;
            heroIcon.textureName = heroData.iconName;
            fragFrame.SetActive(true);

            //隐藏等级
            levelBg.SetActive(false);
            onArrayMark.SetActive(false);
            goodAt.SetActive(false);
            SetQualityColor(quality);
        }

        public void SetFragWithId(string id)
        {
            TableFrag fragdata = DictFragBlo.GetTableFrag(id);
            if (fragdata == null)
                return;
            UIFragItem item = new UIFragItem(fragdata);
            item.num = 0;
            SetFrag(item);
        }
        
        public long UpdateFragNum(int num = 0)
        {
            if(num <= 0)
            {
                long count = StaticData.playerData.GetFragHeros(heroGuid).num;
                fragNum.text = count.ToString();
                return count;
            }
            else
            {
                fragNum.text = num.ToString();
            }
            return num;
        }

        public void UpdateRedPointForFrag(UIFragItem frag)
        {
            if (frag.num >= frag.fragData.num)
                redPoint.SetActive(true);
            else
                redPoint.SetActive(false);
        }

        public void SetLevel(int level)
        {
            levelBg.SetActive(true);
            heroLevel.text = level.ToString();
        }


        //一般用于显示经验卡个数
        public void UpdateItemCount(string text, UIWidget.Pivot pivot = UIWidget.Pivot.Center)
        {
            labNum.gameObject.SetActive(true);
            labNum.pivot = pivot;
            labNum.text = text;
        }

        public void UpdateRefine(string text)
        {
            labNum.gameObject.SetActive(true);
            labNum.fontSize = (int)(20/gameObject.transform.localScale.x);
            labNum.color = new Color(1, 1, 1);
            labNum.effectColor = new Color(0, 0, 0);
            labNum.transform.localPosition = new Vector3(labNum.transform.localPosition.x, -34, labNum.transform.localPosition.z);
            labNum.pivot = UIWidget.Pivot.Right;
            labNum.text = text;
        }

        public void OnClick()
        {
            if (m_callback != null)
                m_callback(heroGuid);
        }

        public void SetOnArray()
        {
            onArrayMark.SetActive(true);
            onArrayLabel.text = TableTextBlo.GetText("hero_in_array");
        }

        public void SetOnZhuzhen()
        {
            onArrayMark.SetActive(true);
            onArrayLabel.text = TableTextBlo.GetText("hero_in_zhuzhen");
        }

        public void setGoodAtVisable()
        {
            goodAt.SetActive(true);
        }

        public void SetOnChuZhan()
        {
            onArrayMark.SetActive(true);
            onArrayLabel.text = TableTextBlo.GetText("expedition_battle");
        }
        public void SetChuZhanFalse()
        {
            onArrayMark.SetActive(false);
        }
        public void SetOnZhiXing()
        {
            onArrayMark.SetActive(true);
            onArrayLabel.text = TableTextBlo.GetText("expedition_on_task");
        }

        void SetQualityColor(QualityValue quality)
        {
            switch (quality)
            {
                case QualityValue.WHITE:
                    {
                        iconBg.spriteName = "UI_kuang_big_di_white";
                        iconFrame.spriteName = "UI_kuang_big_bian_white";
                        levelBg.GetComponent<UISprite>().spriteName = "UI_kuang_lv_bi_white";
                        fragFrame.GetComponent<UISprite>().spriteName = "UI_kuang_sui_white";
                    }
                    break;
                case QualityValue.GREEN:
                    {
                        iconBg.spriteName = "UI_kuang_big_di_green";
                        iconFrame.spriteName = "UI_kuang_big_bian_green";
                        levelBg.GetComponent<UISprite>().spriteName = "UI_kuang_lv_bi_green";
                        fragFrame.GetComponent<UISprite>().spriteName = "UI_kuang_sui_green";
                    }
                    break;
                case QualityValue.BLUE:
                    {
                        iconBg.spriteName = "UI_kuang_big_di_blue";
                        iconFrame.spriteName = "UI_kuang_big_bian_blue";
                        levelBg.GetComponent<UISprite>().spriteName = "UI_kuang_lv_bi_blue";
                        fragFrame.GetComponent<UISprite>().spriteName = "UI_kuang_sui_blue";
                    }
                    break;
                case QualityValue.PURPLE:
                    {
                        iconBg.spriteName = "UI_kuang_big_di_zi";
                        iconFrame.spriteName = "UI_kuang_big_bian_zi";
                        levelBg.GetComponent<UISprite>().spriteName = "UI_kuang_lv_bi_zi";
                        fragFrame.GetComponent<UISprite>().spriteName = "UI_kuang_sui_zi";
                    }
                    break;
                case QualityValue.YELLOW:
                    {
                        iconBg.spriteName = "UI_kuang_big_di_yellow";
                        iconFrame.spriteName = "UI_kuang_big_bian_yellow";
                        levelBg.GetComponent<UISprite>().spriteName = "UI_kuang_lv_bi_yellow";
                        fragFrame.GetComponent<UISprite>().spriteName = "UI_kuang_sui_yellow";
                    }
                    break;
                case QualityValue.RED:
                    {
                        iconBg.spriteName = "UI_kuang_big_di_red";
                        iconFrame.spriteName = "UI_kuang_big_bian_red";
                        levelBg.GetComponent<UISprite>().spriteName = "UI_kuang_lv_bi_red";
                        fragFrame.GetComponent<UISprite>().spriteName = "UI_kuang_sui_red";
                    }
                    break;
                default:
                    {
                        iconBg.spriteName = "UI_kuang_big_di_white";
                        iconFrame.spriteName = "UI_kuang_big_bian_white";
                        levelBg.GetComponent<UISprite>().spriteName = "UI_kuang_lv_bi_white";
                        fragFrame.GetComponent<UISprite>().spriteName = "UI_kuang_sui_white";
                    }
                    break;
            }
        }
    }

}